//
//  GameViewController.m
//  Dear
//
//  Created by 陈俊俊 on 15/7/19.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "GameViewController.h"
#import "GameCell.h"
#import "GameModel.h"
#import "GGameView.h"
@interface GameViewController ()<UITableViewDataSource,UITableViewDelegate,GGameViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArr;
    GGameView *_gameView;
}
@end

@implementation GameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取数据
    [self getData];
    //创建tableView
    [self createTableView];

}
#pragma mark - 获取数据
- (void)getData
{
    _dataArr = [[NSMutableArray alloc]init];
    //游戏
    NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId,@"flag":self.flag};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpGetPersonGame];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0)
            {
                NSArray *items = dict[@"item"];
                for (NSDictionary *itemDict in items) {
                    GameModel *model  = [[GameModel alloc]init];
                    [model setValuesForKeysWithDictionary:itemDict];
                    [_dataArr addObject:model];
                }
                [_tableView reloadData];
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];

}
#pragma mark - 创建tableView
- (void)createTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width,ScreenSize.height - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}



#pragma mark - tableView协议的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"GameCell";
    GameCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"GameCell" owner:self options:nil]lastObject];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (_dataArr.count > 0) {
        cell.model = _dataArr[indexPath.row];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GameModel *model = _dataArr[indexPath.row];
    CGFloat conH = 0;
    if (model.content.length > 0) {
        conH = [CJJHelper textHeightFromString:model.content width:ScreenSize.width - 30 fontSize:15];
    }
    if (conH + 59 > 90) {
        return conH + 59 + 10;
    }
    return 90;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GameModel *model = _dataArr[indexPath.row];
    if (_gameView == nil) {
        _gameView = [[[NSBundle mainBundle]loadNibNamed:@"GGameView" owner:self options:nil]lastObject];
        _gameView.model = model;
        _gameView.delegate = self;
        [[DearAppDelegate window] addSubview:_gameView];
    }
}

- (void)dissmiss{
    if (_gameView) {
        [_gameView removeFromSuperview];
        _gameView = nil;
    }
}

- (void)downloadWith:(NSString *)url{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
